package com.pinapel.aixml.ui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.plaf.basic.BasicButtonUI;
import javax.swing.text.JTextComponent;

import com.pinapel.aixml.controller.TabbedLogController;
import com.pinapel.aixml.tools.i18n.I18n;

public class TabbedLogUI extends JTabbedPane {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6034980068755651197L;

	private LogUI mLogUI;
	private LogInfoVerboseUI mVerbose;
	private TabbedLogController mController;

	public TabbedLogUI() {
		this.setBorder(javax.swing.BorderFactory
				.createLineBorder(new java.awt.Color(Color.TRANSLUCENT)));

		mLogUI = new LogUI();
		mVerbose = new LogInfoVerboseUI();

		// setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);

		this.addTab(I18n.getString(I18n.UI006_LOG), mLogUI);
		this.addTab(I18n.getString(I18n.UI006_LOG_EXTENDED), mVerbose);

		TabComponent buttonLogUI = new TabComponent(this, mLogUI.getTextArea());
		TabComponent buttonLogInfoUI = new TabComponent(this,
				mVerbose.getTextArea());

		setTabComponentAt(0, buttonLogUI);
		setTabComponentAt(1, buttonLogInfoUI);
	}

	public TabbedLogController getController() {
		return mController;
	}

	public void setController(TabbedLogController controller) {
		mController = controller;
	}

	public LogUI getLogUI() {
		return mLogUI;
	}

	public LogInfoVerboseUI getInfoVerboseUI() {
		return mVerbose;
	}

	private class TabComponent extends JPanel {
		private final JTabbedPane mPane;
		private final JTextComponent mTextArea;

		public TabComponent(JTabbedPane pane, JTextComponent text) {
			// unset default FlowLayout' gaps
			super(new FlowLayout(FlowLayout.LEFT, 0, 0));
			if (pane == null) {
				throw new NullPointerException("TabbedPane is null");
			}
			mPane = pane;
			if (text == null) {
				throw new NullPointerException("Text to clean is null");
			}
			mTextArea = text;

			setOpaque(false);
			// make JLabel read titles from JTabbedPane
			JLabel label = new JLabel() {
				public String getText() {
					int i = mPane.indexOfTabComponent(TabComponent.this);
					if (i != -1) {
						return mPane.getTitleAt(i);
					}
					return null;
				}
			};
			add(label);

			JButton button = new TabButton(mTextArea);
			add(button);
		}

		private class TabButton extends JButton implements ActionListener {
			private JTextComponent mTextArea;

			public TabButton(JTextComponent textArea) {
				mTextArea = textArea;
				int size = 17;
				setPreferredSize(new Dimension(size, size));
				setToolTipText("Clear this tab");
				// Make the button looks the same for all Laf's
				setUI(new BasicButtonUI());
				// Make it transparent
				setContentAreaFilled(false);
				// No need to be focusable
				setFocusable(false);
				setBorder(BorderFactory.createEtchedBorder());
				setBorderPainted(false);
				// Making nice rollover effect
				// we use the same listener for all buttons
				setRolloverEnabled(true);
				// Close the proper tab by clicking the button
				addActionListener(this);
			}

			public void actionPerformed(ActionEvent e) {
				mTextArea.setText("");
			}

			// paint the cross
			protected void paintComponent(Graphics g) {
				super.paintComponent(g);
				Graphics2D g2 = (Graphics2D) g.create();
				// shift the image for pressed buttons
				if (getModel().isPressed()) {
					g2.translate(1, 1);
				}
				g2.setStroke(new BasicStroke(2));
				g2.setColor(Color.BLACK);
				if (getModel().isRollover()) {
					g2.setColor(Color.DARK_GRAY);
				}
				int delta = 6;
				g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight()
						- delta - 1);
				g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight()
						- delta - 1);
				g2.dispose();
			}
		}
	}
}
